package net.entelijan.cobean.bind.impl.remove;

import org.junit.Assert;
import org.junit.Test;

public class RecordingCobeanTestcase {

    @Test
    public void testSetSameValue() {
        InvisibleCobean rc = new InvisibleCobean();
        final StringBuffer events = new StringBuffer();
        rc.setPropertyChangeListener(new RecordingPropertyChangeListener(events, "irrelevant"));
        rc.setValue("A");
        rc.setValue("A");
        rc.setValue("A");
        rc.setValue("B");
        rc.setValue("B");
        rc.setValue("A");
        rc.setValue("A");
        rc.setValue("A");
        Assert.assertEquals("1nullA1AB1BA", events.toString());
    }

    @Test
    public void testDiffrentValue() {
        InvisibleCobean rc = new InvisibleCobean();
        final StringBuffer events = new StringBuffer();
        rc.setPropertyChangeListener(new RecordingPropertyChangeListener(events, "irrelevant"));
        rc.setValue("A");
        rc.setValue("B");
        rc.setValue("A");
        Assert.assertEquals("1nullA1AB1BA", events.toString());
    }


    @Test
    public void testDiffrentValueRemoving() {
        InvisibleCobean rc = new InvisibleCobean();
        final StringBuffer events = new StringBuffer();
        rc.setPropertyChangeListener(new RemovingRecordingPropertyChangeListener(events, "irrelevant"));
        rc.setValue("A");
        rc.setValue("B");
        rc.setValue("A");
        Assert.assertEquals("1nullA1AB1BA", events.toString());
    }

    @Test
    public void testSetAnotherListener() {
        InvisibleCobean rc = new InvisibleCobean();
        final StringBuffer events = new StringBuffer();
        rc.setPropertyChangeListener(new RecordingPropertyChangeListener("1", events, "irrelevant"));
        rc.setValue("A");
        rc.setValue("B");
        rc.setValue("A");
        rc.setPropertyChangeListener(new RecordingPropertyChangeListener("2", events, "irrelevant"));
        rc.setValue("B");
        rc.setValue("A");
        Assert.assertEquals("1nullA1AB1BAR2AB2BA", events.toString());
    }

    @Test
    public void testSetAnotherListenerRemoving() {
        InvisibleCobean rc = new InvisibleCobean();
        final StringBuffer events = new StringBuffer();
        rc.setPropertyChangeListener(new RemovingRecordingPropertyChangeListener("1", events, "irrelevant"));
        rc.setValue("A");
        rc.setValue("B");
        rc.setValue("A");
        rc.setPropertyChangeListener(new RemovingRecordingPropertyChangeListener("2", events, "irrelevant"));
        rc.setValue("B");
        rc.setValue("A");
        Assert.assertEquals("1nullA1AB1BA1R2AB2BA", events.toString());
    }

    @Test
    public void testSetAnotherListenerRemovingTwoTimes() {
        InvisibleCobean rc = new InvisibleCobean();
        final StringBuffer events = new StringBuffer();
        rc.setPropertyChangeListener(new RemovingRecordingPropertyChangeListener("1", events, "irrelevant"));
        rc.setValue("A");
        rc.setValue("B");
        rc.setValue("A");
        rc.setPropertyChangeListener(new RemovingRecordingPropertyChangeListener("2", events, "irrelevant"));
        rc.setValue("B");
        rc.setValue("A");
        rc.setPropertyChangeListener(new RemovingRecordingPropertyChangeListener("3", events, "irrelevant"));
        rc.setValue("B");
        rc.setValue("B");
        rc.setValue("B");
        rc.setValue("A");
        rc.setValue("A");
        rc.setValue("A");
        rc.setValue("A");
        Assert.assertEquals("1nullA1AB1BA1R2AB2BA2R3AB3BA", events.toString());
    }

}
